//
//  RectangleCornerArcView.m
//  iOSStudy
//
//  Created by Evan Xie on 11/30/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "RectangleCornerArcView.h"


@implementation RectangleCornerArcView


- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    CGContextSaveGState(context);
    
    // Stroke a square with red color
    CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
    
    CGFloat dash[2] = {3.0, 3.0};
    CGContextSetLineDash(context, 10, dash, 2);
    
    CGFloat size = MIN(rect.size.width, rect.size.height);
    CGRect squareFrame = CGRectMake(20, 20, size-40, size-40);
    CGContextAddRect(context, squareFrame);
    CGContextStrokePath(context);
    
    CGContextRestoreGState(context);
    
    /**
        ________
     1 |        | 2
       |        |
       |        |
     4 |________| 3
     
     */
    
    CGContextSetLineWidth(context, 2.0);
    
    // First, draw the left-top corner arc clockwise.
    CGFloat cornerRadius = 20.0;
    CGContextMoveToPoint(context, squareFrame.origin.x, squareFrame.origin.y + cornerRadius);
    CGContextAddArcToPoint(context, squareFrame.origin.x, squareFrame.origin.y, CGRectGetMaxX(squareFrame), squareFrame.origin.y, cornerRadius);
    CGContextSetRGBStrokeColor(context, 0, 1.0, 0, 1.0);
    CGContextStrokePath(context);
    
    // Now, we try to draw the left-bottom corner arc counterclockwise.
    CGContextMoveToPoint(context, squareFrame.origin.x, CGRectGetMaxY(squareFrame) - cornerRadius);
    CGContextAddArcToPoint(context, squareFrame.origin.x, CGRectGetMaxY(squareFrame), CGRectGetMaxX(squareFrame), CGRectGetMaxY(squareFrame), cornerRadius);
    CGContextSetRGBStrokeColor(context, 0, 0, 1.0, 1.0);
    CGContextStrokePath(context);
    
    // Draw the right-top corner arc
    CGContextMoveToPoint(context, CGRectGetMaxX(squareFrame) - cornerRadius, squareFrame.origin.y);
    CGContextAddArcToPoint(context, CGRectGetMaxX(squareFrame), squareFrame.origin.y, CGRectGetMaxX(squareFrame), squareFrame.origin.y + cornerRadius, cornerRadius);
    CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
    CGContextStrokePath(context);
    
    // Draw the right-bottom corner arc
    CGContextMoveToPoint(context, CGRectGetMaxX(squareFrame), CGRectGetMaxY(squareFrame) - cornerRadius);
    CGContextAddArcToPoint(context, CGRectGetMaxX(squareFrame), CGRectGetMaxY(squareFrame), squareFrame.origin.x, CGRectGetMaxY(squareFrame), cornerRadius);
    CGContextSetStrokeColorWithColor(context, [UIColor magentaColor].CGColor);
    CGContextStrokePath(context);
}

@end
